package com.wmlt.util;

import org.commonmark.Extension;
import org.commonmark.ext.gfm.tables.TableBlock;
import org.commonmark.ext.gfm.tables.TablesExtension;
import org.commonmark.ext.heading.anchor.HeadingAnchorExtension;
import org.commonmark.node.Document;
import org.commonmark.node.Link;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.AttributeProvider;
import org.commonmark.renderer.html.AttributeProviderContext;
import org.commonmark.renderer.html.AttributeProviderFactory;
import org.commonmark.renderer.html.HtmlRenderer;
import java.util.*;
/**
 * @author liyuxiang
 * @description: MarkDown转换html工具类
 * @param:
 * @return:
 * @date: 2021/3/25 9:23 下午
 */
public class MarkDownUtil extends Thread {
    /**
     * markdown格式转换成HTML格式
     *
     * @param markdown
     * @return
     */
    public String markdown;

    public MarkDownUtil(String markdown) {
        this.markdown = markdown;
    }
    public MarkDownUtil() {

    }

    public static String markdownToHtml(String markdown) {
        Parser parser = Parser.builder().build();
        Node document = parser.parse(markdown);
        HtmlRenderer renderer = HtmlRenderer.builder().build();
        return renderer.render(document);
    }

    /**
     * 增加扩展[标题锚点，表格生成]
     * Markdown转换成HTML
     *
     * @param markdown
     * @return
     */
    public static String markdownToHtmlExtensions(String markdown) {
        //h标题生成id
        Set<Extension> headingAnchorExtensions = Collections.singleton(HeadingAnchorExtension.create());
        //转换table的HTML
        List<Extension> tableExtension = Arrays.asList(TablesExtension.create());
        Parser parser = Parser.builder()
                .extensions(tableExtension)
                .build();
        Node document = parser.parse(markdown);
        HtmlRenderer renderer = HtmlRenderer.builder()
                .extensions(headingAnchorExtensions)
                .extensions(tableExtension)
                .attributeProviderFactory(new AttributeProviderFactory() {
                    public AttributeProvider create(AttributeProviderContext context) {
                        return new CustomAttributeProvider();
                    }
                })
                .build();
        return renderer.render(document);
    }

    /**
     * 处理标签的属性
     */
    static class CustomAttributeProvider implements AttributeProvider {
        @Override
        public void setAttributes(Node node, String tagName, Map<String, String> attributes) {
            //改变a标签的target属性为_blank
            if (node instanceof Link) {
                attributes.put("target", "_blank");
            }
            if (node instanceof TableBlock) {
                attributes.put("class", "ui celled table");
            }
        }
    }


    public static void main(String[] args) {
        String a = "[百度](http://www.baidu.com)";
        System.out.println(markdownToHtmlExtensions(a));
        System.out.println(markdownToHtml(a));
    }

    @Override
    public synchronized void run() {
        //h标题生成id
        Set<Extension> headingAnchorExtensions = Collections.singleton(HeadingAnchorExtension.create());
        //转换table的HTML
        List<Extension> tableExtension = Arrays.asList(TablesExtension.create());
        Parser parser = Parser.builder()
                .extensions(tableExtension)
                .build();
        Node document = parser.parse(markdown);
        HtmlRenderer renderer = HtmlRenderer.builder()
                .extensions(headingAnchorExtensions)
                .extensions(tableExtension)
                .attributeProviderFactory(new AttributeProviderFactory() {
                    public AttributeProvider create(AttributeProviderContext context) {
                        return new CustomAttributeProvider();
                    }
                })
                .build();
       markdown=renderer.render(document);
        System.out.println(markdown);
    }
//    public static String htmlToText(String markdown){
//        Document document = Jsoup.parse(markdown);
//        Document.OutputSettings outputSettings = new Document.OutputSettings().prettyPrint(false);
//        document.outputSettings(outputSettings);
//        document.select("br").append("\\n");
//        document.select("p").prepend("\\n");
//        document.select("p").append("\\n");
//        String newHtml = document.html().replaceAll("\\\\n", "\n");
//        String plainText = Jsoup.clean(newHtml, "", Whitelist.none(), outputSettings);
//        String result = StringEscapeUtils.unescapeHtml(plainText.trim());
//        return result;
//    }
}